// 主应用程序控制器
class GamePlatform {
    constructor() {
        this.currentScreen = 'login-screen';
        this.initializeApp();
    }

    // 初始化应用
    async initializeApp() {
        // 检查是否已登录
        const isLoggedIn = await authManager.checkAuthStatus();
        
        if (isLoggedIn) {
            this.showGameMenu();
        } else {
            this.showLoginScreen();
        }

        this.bindEvents();
        
        // 初始化管理员面板
        adminPanel = new AdminPanel();
    }

    // 显示注册界面
    showRegisterScreen() {
        this.switchScreen('register-screen');
    }

    // 显示登录界面
    showLoginScreen() {
        this.switchScreen('login-screen');
    }

    // 显示游戏菜单
    showGameMenu() {
        const user = authManager.getCurrentUser();
        document.getElementById('current-user').textContent = user.username;
        
        const roleEl = document.getElementById('user-role');
        roleEl.textContent = user.role === 'admin' ? '管理员' : '用户';
        roleEl.className = `role-badge ${user.role === 'admin' ? 'admin' : ''}`;
        
        // 显示/隐藏管理员按钮
        const adminBtn = document.getElementById('admin-panel-btn');
        if (authManager.isAdmin()) {
            adminBtn.style.display = 'block';
        } else {
            adminBtn.style.display = 'none';
        }
        
        this.switchScreen('game-menu');
    }

    // 显示管理员面板
    showAdminPanel() {
        if (!authManager.isAdmin()) {
            showMessage('需要管理员权限', 'error');
            return;
        }
        this.switchScreen('admin-panel');
        adminPanel.show();
    }

    // 显示游戏界面
    showGame(gameName) {
        switch (gameName) {
            case 'tetris':
                this.switchScreen('tetris-game');
                if (!tetrisGame) {
                    tetrisGame = new TetrisGame();
                }
                break;
            case 'match3':
                this.switchScreen('match3-game');
                if (!match3Game) {
                    match3Game = new Match3Game();
                }
                break;
            case 'minesweeper':
                this.switchScreen('minesweeper-game');
                if (!minesweeperGame) {
                    minesweeperGame = new MinesweeperGame();
                }
                break;
            case 'snake':
                this.switchScreen('snake-game');
                if (!snakeGame) {
                    snakeGame = new SnakeGame();
                }
                break;
            case 'flappy':
                this.switchScreen('flappy-game');
                if (!flappyGame) {
                    flappyGame = new FlappyGame();
                }
                break;
            case 'memory':
                this.switchScreen('memory-game');
                if (!memoryGame) {
                    memoryGame = new MemoryGame();
                }
                break;
        }
    }

    // 切换界面
    switchScreen(screenId) {
        // 隐藏所有界面
        document.querySelectorAll('.screen').forEach(screen => {
            screen.classList.remove('active');
        });

        // 显示目标界面
        document.getElementById(screenId).classList.add('active');
        this.currentScreen = screenId;
    }

    // 绑定事件
    bindEvents() {
        // 注册表单提交
        document.getElementById('register-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const username = document.getElementById('reg-username').value;
            const password = document.getElementById('reg-password').value;
            const confirmPassword = document.getElementById('reg-confirm-password').value;

            const result = await authManager.register(username, password, confirmPassword);
            
            if (result.success) {
                showMessage(result.message, 'success');
                this.showLoginScreen();
                // 清空注册表单
                document.getElementById('register-form').reset();
            } else {
                showMessage(result.message, 'error');
            }
        });

        // 登录表单提交
        document.getElementById('login-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            const result = await authManager.login(username, password);
            
            if (result.success) {
                showMessage(result.message, 'success');
                this.showGameMenu();
            } else {
                showMessage(result.message, 'error');
            }
        });

        // 切换到注册界面
        document.getElementById('show-register').addEventListener('click', (e) => {
            e.preventDefault();
            this.showRegisterScreen();
        });

        // 切换到登录界面
        document.getElementById('show-login').addEventListener('click', (e) => {
            e.preventDefault();
            this.showLoginScreen();
        });

        // 退出登录
        document.getElementById('logout-btn').addEventListener('click', async () => {
            const result = await authManager.logout();
            if (result.success) {
                showMessage('已退出登录', 'info');
                this.showLoginScreen();
                // 清空登录表单
                document.getElementById('login-form').reset();
            }
        });

        // 管理员面板按钮
        document.getElementById('admin-panel-btn').addEventListener('click', () => {
            this.showAdminPanel();
        });

        // 游戏卡片点击
        document.querySelectorAll('.game-card').forEach(card => {
            card.addEventListener('click', () => {
                const gameName = card.dataset.game;
                this.showGame(gameName);
            });
        });

        // 返回菜单按钮
        document.getElementById('back-to-menu').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 消消乐返回菜单
        document.getElementById('back-to-menu-match3').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 扫雷返回菜单
        document.getElementById('back-to-menu-minesweeper').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 贪吃蛇返回菜单
        document.getElementById('back-to-menu-snake').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 飞翔小鸟返回菜单
        document.getElementById('back-to-menu-flappy').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 记忆翻牌返回菜单
        document.getElementById('back-to-menu-memory').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 管理员面板返回按钮
        document.getElementById('back-to-menu-admin').addEventListener('click', () => {
            this.showGameMenu();
        });

        // 俄罗斯方块游戏控制
        document.getElementById('start-game').addEventListener('click', () => {
            if (tetrisGame) {
                tetrisGame.startGame();
            }
        });

        document.getElementById('pause-game').addEventListener('click', () => {
            if (tetrisGame) {
                tetrisGame.togglePause();
            }
        });

        // 消消乐游戏控制
        document.getElementById('start-match3').addEventListener('click', () => {
            if (match3Game) {
                match3Game.startGame();
            }
        });

        document.getElementById('restart-match3').addEventListener('click', () => {
            if (match3Game) {
                match3Game.restartGame();
            }
        });

        // 扫雷游戏控制
        document.getElementById('start-minesweeper').addEventListener('click', () => {
            if (minesweeperGame) {
                minesweeperGame.startGame();
            }
        });

        // 贪吃蛇游戏控制
        document.getElementById('start-snake').addEventListener('click', () => {
            if (snakeGame) {
                snakeGame.startGame();
            }
        });

        // 飞翔小鸟游戏控制
        document.getElementById('start-flappy').addEventListener('click', () => {
            if (flappyGame) {
                flappyGame.startGame();
            }
        });

        // 记忆翻牌游戏控制
        document.getElementById('start-memory').addEventListener('click', () => {
            if (memoryGame) {
                memoryGame.startGame();
            }
        });
    }
}

// 当页面加载完成后启动应用
document.addEventListener('DOMContentLoaded', () => {
    new GamePlatform();
});